package com.lagou.edu.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/autodeliver")
public class AutodeliverController {


    @Autowired
    private RestTemplate restTemplate;

    // /autodeliver/checkState/1545132
    /*@GetMapping("/checkState/{userId}")
    public Integer findResumeOpenState(@PathVariable Long userId) {
        // 调用远程服务—> 简历微服务接口  RestTemplate  -> JdbcTempate
        // httpclient封装好多内容进行远程调用
        Integer forObject = restTemplate.getForObject("http://localhost:8080/resume/openstate/" + userId, Integer.class);
        return forObject;
    }*/

    @Autowired
    private DiscoveryClient discoveryClient;

    /*@GetMapping("/checkState/{userId}")
    public Integer findResumeOpenState(@PathVariable Long userId) {
        // 调用远程服务—> 简历微服务接口  RestTemplate  -> JdbcTempate
        // httpclient封装好多内容进行远程调用
        List<ServiceInstance> instances = discoveryClient.getInstances("lagou-service-resume");
        ServiceInstance serviceInstance = instances.get(0);
        Integer forObject = restTemplate.getForObject("http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/resume/openstate/" + userId, Integer.class);
        return forObject;
    }*/

    @GetMapping("/checkState/{userId}")
    public Integer findResumeOpenState(@PathVariable Long userId) {
        // 调用远程服务—> 简历微服务接口  RestTemplate  -> JdbcTempate
        // httpclient封装好多内容进行远程调用
        Integer forObject = restTemplate.getForObject("http://lagou-service-resume/resume/openstate/" + userId, Integer.class);
        return forObject;
    }

    // /autodeliver/checkState/1545132
    @GetMapping("/checkStateTimeout/{userId}")
    @HystrixCommand(
            threadPoolKey = "findResumeOpenStateTimeout",
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "1"),
                    @HystrixProperty(name = "maxQueueSize", value = "20")
            },
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
            })
    public Integer findResumeOpenStateTimeout(@PathVariable Long userId) {
        // 调用远程服务—> 简历微服务接口  RestTemplate  -> JdbcTempate
        // httpclient封装好多内容进行远程调用
        Integer forObject = restTemplate.getForObject("http://lagou-service-resume/resume/openstate/" + userId, Integer.class);
        return forObject;
    }

    // /autodeliver/checkStateTimeoutFallback/1545132
    @GetMapping("/checkStateTimeoutFallback/{userId}")
    @HystrixCommand(
            threadPoolKey = "findResumeOpenStateTimeoutFallback",
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "2"),
                    @HystrixProperty(name = "maxQueueSize", value = "20")
            },
            commandProperties = {
                    //超时时间设置
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000"),
                    // hystrix⾼级配置，定制⼯作过程细节
                    // 统计时间窗⼝定义
                    @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "8000"),
                    // 统计时间窗⼝内的最⼩请求数
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2"),
                    // 统计时间窗⼝内的错误数量百分⽐阈值
                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"),
                    // ⾃我修复时的活动窗⼝⻓度
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "3000")
            }, fallbackMethod = "myFallback"
    )
    public Integer findResumeOpenStateTimeoutFallback(@PathVariable Long userId) {
        // 调用远程服务—> 简历微服务接口  RestTemplate  -> JdbcTempate
        // httpclient封装好多内容进行远程调用
        Integer forObject = restTemplate.getForObject("http://lagou-service-resume/resume/openstate/" + userId, Integer.class);
        return forObject;
    }

    public Integer myFallback(Long userId) {
        return -1;
    }

}
